package com.wiken.commentservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wiken.commentservice.DTO.CommentStoreDTO;
import com.wiken.commentservice.DTO.CommentUserDTO;
import com.wiken.commentservice.entity.Comment;
import com.wiken.commentservice.service.impl.CommentServiceImpl;
import com.wiken.servicecommon.common.Code;
import com.wiken.servicecommon.common.PageUtill;
import com.wiken.servicecommon.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wiken
 * @since 2022-12-28
 */
@RestController
@RequestMapping("/commentservice/comment")
public class CommentController {
    @Autowired
    CommentServiceImpl commentService;

    @PostMapping("addComment")
    public R addCommentOfCommodity(@RequestBody Comment comment, @RequestHeader("Authorization") String token){
        boolean success = commentService.addComment(comment, token);

        if(!success) return R.error().code(Code.ERROR).message("评论失败");
        return R.ok().code(Code.SUCCESS).message("评论成功");
    }

    @DeleteMapping("delComment")
    public R delComment(@RequestBody Comment comment, @RequestHeader("Authorization") String token){
        boolean success = commentService.delComment(comment, token);

        if(!success) return R.error().code(Code.ERROR).message("删除评论失败");
        return R.ok().code(Code.SUCCESS).message("删除评论成功");
    }

    @PostMapping("getMyComment")
    public R getMyComment(@RequestBody Comment comment, @RequestHeader("Authorization") String token){
        PageUtill pageUtill = new PageUtill();


        Page<CommentStoreDTO> myComments = commentService.getMyComment(comment, pageUtill, token);

        return R.ok().code(Code.SUCCESS).message("获取成功").data("commentList", myComments);
    }

    @PostMapping("getStoreComment")
    public R getStoreComment(@RequestBody String storeId, @RequestHeader("Authorization") String token){

        Page<CommentUserDTO> commentList = commentService.getCommentByStoreId(storeId, token, new PageUtill());

        return R.ok().code(Code.SUCCESS).message("获取成功").data("commentList", commentList);
    }
}
